<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <!--      <el-form-item label="日报表id外键" prop="reportId">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.reportId"-->
      <!--          placeholder="请输入日报表id外键"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="工单id外键" prop="ordersId">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.ordersId"-->
      <!--          placeholder="请输入工单id外键"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item label="工单产品" prop="cpName">
        <plans-com v-model="queryParams.cpName" clearable @update:parameterList="handleMaterialNumberUpdate"
                   placeholder="请选择产品"/>

        <!--        <el-select v-model="queryParams.cpName" placeholder="工单产品" clearable-->
        <!--                   @keyup.enter.native="handleQuery">-->
        <!--          <el-option v-for="item in ordersList" :label="item.cpName" :value="item.cpName"-->
        <!--                     :key="item.ordersId"></el-option>-->
        <!--        </el-select>-->
      </el-form-item>
      <!--      <el-form-item label="计划开始时间" prop="startTime">-->
      <!--        <el-date-picker clearable-->
      <!--                        v-model="queryParams.startTime"-->
      <!--                        type="date"-->
      <!--                        value-format="yyyy-MM-dd"-->
      <!--                        placeholder="请选择计划开始时间">-->
      <!--        </el-date-picker>-->
      <!--      </el-form-item>-->
      <el-form-item label="计划完成时间" prop="completionTime">
        <el-date-picker clearable
                        v-model="queryParams.completionTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择计划完成时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="总工时" prop="plannedTime">
        <el-input
          v-model="queryParams.plannedTime"
          placeholder="请输入这批货的计划总工时"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="责任人" prop="personCharge">
        <el-input
          v-model="queryParams.personCharge"
          placeholder="请输入责任人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['production:plans:add']"
        >新增
        </el-button>
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['production:plans:edit']"
        >修改
        </el-button>
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['production:plans:export']"
        >导出
        </el-button>
      </el-form-item>
    </el-form>

    <!--    <el-row :gutter="10" class="mb8">-->
    <!--      <el-col :span="1.5">-->

    <!--      </el-col>-->
    <!--      <el-col :span="1.5">-->

    <!--      </el-col>-->
    <!--      <el-col :span="1.5">-->
    <!--        &lt;!&ndash;        <el-button&ndash;&gt;-->
    <!--        &lt;!&ndash;          type="danger"&ndash;&gt;-->
    <!--        &lt;!&ndash;          plain&ndash;&gt;-->
    <!--        &lt;!&ndash;          icon="el-icon-delete"&ndash;&gt;-->
    <!--        &lt;!&ndash;          size="mini"&ndash;&gt;-->
    <!--        &lt;!&ndash;          :disabled="multiple"&ndash;&gt;-->
    <!--        &lt;!&ndash;          @click="handleDelete"&ndash;&gt;-->
    <!--        &lt;!&ndash;          v-hasPermi="['production:plans:remove']"&ndash;&gt;-->
    <!--        &lt;!&ndash;        >删除&ndash;&gt;-->
    <!--        &lt;!&ndash;        </el-button>&ndash;&gt;-->
    <!--      </el-col>-->
    <!--      <el-col :span="1.5">-->

    <!--      </el-col>-->
    <!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
    <!--    </el-row>-->

    <el-table v-loading="loading" :data="plansList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <!--      <el-table-column label="生产计划id" align="center" prop="plansId" />-->
      <!--      <el-table-column label="日报表id外键" align="center" prop="reportId" />-->
      <!--      <el-table-column label="工单id外键" align="center" prop="ordersId"/>-->
      <el-table-column label="生产工单产品" align="center" prop="cpName" width="200" show-overflow-tooltip/>
      <el-table-column label="产品数量" align="center" prop="scSum"/>
      <el-table-column label="完成进度" align="center" prop="gzSchedulePercentage"/>
      <el-table-column label="计划开始时间" align="center" prop="startTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="计划完成时间" align="center" prop="completionTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.completionTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="计划总工时（时）" align="center" prop="durationHours" width="150"/>
      <el-table-column label="责任人" align="center" prop="personCharge"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['production:plans:edit']"
          >修改
          </el-button>
          <!--          <el-button-->
          <!--            size="mini"-->
          <!--            type="text"-->
          <!--            icon="el-icon-delete"-->
          <!--            @click="handleDelete(scope.row)"-->
          <!--            v-hasPermi="['production:plans:remove']"-->
          <!--          >删除-->
          <!--          </el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改生产计划对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <!--        <el-form-item label="日报表id外键" prop="reportId">-->
        <!--          <el-input v-model="form.reportId" placeholder="请输入日报表id外键" />-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="工单id外键" prop="ordersId">-->
        <!--          <el-input v-model="form.ordersId" placeholder="请输入工单id外键"/>-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="产品名称" prop="cpName">-->
        <!--          <el-input v-model="form.cpName" placeholder="请输入产品名称"/>-->
        <!--        </el-form-item>-->
        <el-row>
          <el-col :span="12">

            <el-form-item label="生产工单产品" prop="cpName">
              <plans-com v-model="form.cpName" @update:parameterList="handleMaterialNumberUpdate"
                         placeholder="请选择产品" clearable/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="计划完成时间" prop="completionTime">
              <el-date-picker clearable
                              style="width: 100%"
                              v-model="form.completionTime"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择计划完成时间">
              </el-date-picker>
            </el-form-item>
            <!--            <el-form-item label="计划工时(时)" prop="plannedTime">-->
            <!--              <el-input v-model="form.plannedTime" placeholder="请输入这批货的计划总工时"-->
            <!--                        oninput="value=value.replace(/[^\d.]/g,'')"/>-->
            <!--            </el-form-item>-->
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="计划开始时间" prop="startTime">
              <el-date-picker clearable
                              style="width: 100%"
                              v-model="form.startTime"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择计划开始时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">

          </el-col>
        </el-row>
        <!--        <el-form-item label="责任人" prop="personCharge">-->
        <!--          <el-input v-model="form.personCharge" placeholder="请输入责任人"/>-->
        <!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listPlans, getPlans, delPlans, addPlans, updatePlans} from "@/api/production/plans";
import {listOrders} from "@/api/production/orders";
import PlansCom from "@/views/production/plans/plansCom.vue";

export default {
  name: "Plans",
  components: {PlansCom},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 生产计划表格数据
      plansList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        reportId: null,
        ordersId: null,
        cpName: null,
        startTime: null,
        completionTime: null,
        plannedTime: null,
        personCharge: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        cpName: [
          {required: true, trigger: "blur", message: "请选择产品名称"}
        ],
        number: [
          {required: true, trigger: "blur", message: "请输入产品数量"}
        ],
      },
      ordersList: null
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleMaterialNumberUpdate(parameterList) {
      this.parameterList = parameterList;
      this.form.scSum = this.parameterList.scSum
      // 你可以在这里将 materialNumber 传递到其他需要使用的地方
    },

    /** 查询生产计划列表 */
    getList() {
      this.loading = true;
      listPlans(this.queryParams).then(response => {
        this.plansList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        plansId: null,
        reportId: null,
        ordersId: null,
        cpName: null,
        startTime: null,
        completionTime: null,
        plannedTime: null,
        personCharge: null,
        createTime: null,
        scSum: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.plansId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加生产计划";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      console.log(row)
      this.reset();
      const plansId = row.plansId || this.ids
      getPlans(plansId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改生产计划";
      });
    },
    getNumber() {
      console.log(this.form.cpName)
      this.ordersList.forEach(item => {
        console.log('item', item)
        // scSum 需要数量
        if (item.cpName == this.form.cpName) {
          console.log('number', item.scSum)
          this.form.scSum = item.scSum
        }
      })
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.plansId != null) {
            this.form.scSum = this.parameterList.scSum
            updatePlans(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            console.log('user', this.$store.state.user.name)
            this.form.scSum = this.parameterList.scSum
            this.form.personCharge = this.$store.state.user.name
            addPlans(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const plansIds = row.plansId || this.ids;
      this.$modal.confirm('是否确认删除生产计划编号为"' + plansIds + '"的数据项？').then(function () {
        return delPlans(plansIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('production/plans/export', {
        ...this.queryParams
      }, `plans_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
